package cn.cdeden.knowledge.controller.team;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.dto.*;
import cn.cdeden.knowledge.service.FilesCollectionService;
import cn.cdeden.knowledge.service.FilesDistributeService;
import cn.cdeden.knowledge.service.TeamRelevanceTypeService;
import cn.cdeden.knowledge.service.TeamTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author Chen
* @date 2024-04-19
**/
@RestController
@RequiredArgsConstructor
//@Api(tags = "团队分类管理")
@RequestMapping("/api/teamType")
public class TeamTypeController {

    private final TeamTypeService teamTypeService;

    private final TeamRelevanceTypeService teamRelevanceTypeService;

    private final FilesCollectionService filesCollectionService;

    private final FilesDistributeService filesDistributeService;


    @GetMapping
//    @Log("查询团队分类")
//    @ApiOperation("查询团队分类")
    public R<TableDataInfo<TeamTypeDto>> query(TeamTypeQueryParam query, Pageable pageable){
        TableDataInfo<TeamTypeDto> info = teamTypeService.queryAll(query,pageable);
        return R.ok(info);
    }

    @PostMapping
//    @Log("新增团队分类")
//    @ApiOperation("新增团队分类")
    public R create(@Validated @RequestBody TeamTypeDto resources){
        Long userId = LoginHelper.getUserId();
        resources.setCreateId(String.valueOf(userId));
        resources.setUpdateId(String.valueOf(userId));
        return R.ok(teamTypeService.insert(resources));
    }

    @PutMapping
//    @Log("修改团队分类")
//    @ApiOperation("修改团队分类")
    public R update(@Validated @RequestBody TeamTypeDto resources){
        return R.ok(teamTypeService.updateById(resources));
    }

    @DeleteMapping
//    @Log("删除团队分类")
//    @ApiOperation("删除团队分类")
    public R delete(@RequestBody Set<Integer> ids) {
        //删除分类的关联关系
        for (Integer id : ids) {
            teamRelevanceTypeService.deleteTypeId(id);
        }
        return R.ok(teamTypeService.deleteByIds(ids));
    }

    @GetMapping("/team-count")
//    @Log("团队门户数量统计")
//    @ApiOperation("团队门户数量统计")
    public R<TeamTitleDto> teamCount(Pageable pageable) {
        TeamTitleDto teamTitleDto = new TeamTitleDto();
        TeamTypeQueryParam teamTypeQueryParamquery = new TeamTypeQueryParam();
        TableDataInfo<TeamTypeDto> info = teamTypeService.queryAll(teamTypeQueryParamquery,pageable);
        teamTitleDto.setTeam(info.getTotal());

        FilesCollectionQueryParam filesCollectionQueryParam = new FilesCollectionQueryParam();
        filesCollectionQueryParam.setFileType("1");
        TableDataInfo<FilesTree> filesTreePageInfo = filesCollectionService.queryAll(filesCollectionQueryParam, pageable);
        teamTitleDto.setCollection(filesTreePageInfo.getTotal());

        FilesDistributeQueryParam filesDistributeQueryParam = new FilesDistributeQueryParam();
        filesDistributeQueryParam.setModules("team");
        TableDataInfo<FilesDistributeDto> filesDistributeDtoPageInfo = filesDistributeService.queryDistribute(filesDistributeQueryParam, pageable);
        teamTitleDto.setDistribute(filesDistributeDtoPageInfo.getTotal());
        return R.ok(teamTitleDto);
    }

    @GetMapping("/team-type-count")
//    @Log("统计各分类下团队的数量")
//    @ApiOperation("统计各分类下团队的数量")
    public R<List<TeamCountDto>> teamTypeCount() {
        List<TeamCountDto> teamCountDtoList = new ArrayList<>();
        Map<Integer, Map<String, Object>> stringIntegerMap = teamTypeService.selectTypeCount();
        for (Map.Entry<Integer, Map<String, Object>> entry : stringIntegerMap.entrySet()) {
            Integer id = entry.getKey();
            Map<String, Object> value = entry.getValue();
            TeamCountDto teamCountDto = new TeamCountDto();
            teamCountDto.setId((long)id);
            teamCountDto.setName((String) value.get("NAME"));
            teamCountDto.setCount(((Number) value.get("count")).longValue());
            teamCountDtoList.add(teamCountDto);
        }
        //只筛选出当前用户的团队
        TeamTypeQueryParam teamTypeQueryParam = new TeamTypeQueryParam();
        List<TeamTypeDto> teamTypeDtos = teamTypeService.queryAll(teamTypeQueryParam);
        teamTypeDtos = teamTypeDtos.stream().filter(teamTypeDto -> teamTypeDto.getCreateId().equals(String.valueOf(LoginHelper.getUserId()))).collect(Collectors.toList());
        List<Long> teamIds = teamTypeDtos
                .stream()
                .filter(teamTypeDto -> teamTypeDto.getCreateId().equals(String.valueOf(LoginHelper.getUserId())))
                .map(teamTypeDto -> teamTypeDto.getId().longValue())
                .collect(Collectors.toList());
        teamCountDtoList = teamCountDtoList.stream().filter(teamCountDto -> teamIds.contains(teamCountDto.getId())).collect(Collectors.toList());
        return R.ok(teamCountDtoList);
    }
    /*
//    @Log("导出团队分类")
//    @ApiOperation("导出团队分类")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('teamType:list')")
    public void download(HttpServletResponse response, TeamTypeQueryParam query) throws IOException {
        teamTypeService.download(teamTypeService.queryAll(query), response);
    }*/

}
